<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Slide extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'slide';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'pic_image_url', // 需在此处追加属性，才会触发获取器 getImagesUrlsAttr
        'use_switch_text'
    ];
    

    
    public function getUseSwitchList()
    {
        return ['0' => __('Use_switch 0'), '1' => __('Use_switch 1')];
    }


    public function getUseSwitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['use_switch']) ? $data['use_switch'] : '');
        $list = $this->getUseSwitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    // 获取器直接转换url
    public function getPicImageUrlAttr($value,$data)
    {
        $url = '';
        if($data['pic_image']){
            // 解析图片地址
            $parsedUrl = parse_url($data['pic_image']);
            // 判断图片地址是否包含域名
            if (isset($parsedUrl['host'])) {
                $url = $data['pic_image'];
            } else {
                $url = complete_url($data['pic_image']);
            }
        }
        return $url;
    }
}
